<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('CBase.php');

class CI_CUser extends CBase {	

    function __construct($p)
    {
        parent::__construct($p);
        
        if(!$this->validate_key()){
            $this->error('Authority Key unavailiable.');
        }        	
        $this->CI->load->model('m_User');        
    }
    
    /*
     * User Login
     */
    public function login(){            
        $this->user = $this->CI->m_User->login($this->req['username'],$this->req['password']);
        if($this->user){            
            $cache_key = $this->create_login_cache();
            $this->CI->m_Log->write($this->user->user_id,'User Login',$this->user->username,'LOGIN');
            $this->show_msg('登陆成功.',array('login_key'=>$cache_key));
        }
        else
            $this->error('登陆失败.');
    }

    /*
     * 注册账号
     */
    public function reg()
    {	        
        if($this->CI->m_User->exists($this->req['username']))
            $this->error('用户已存在.');
        $result = $this->CI->m_User->reg(
                $this->req['username']
                ,$this->req['password'],
                $this->req['email'],
                $this->req['mobile']);
        if($result){
            $this->user = $this->CI->m_User->login($this->req['username'],$this->req['password']);
            $this->CI->m_Log->write($this->user->user_id,'User Register',$this->user->username,'REG');
            $cache_key = $this->create_login_cache();                 
            $this->show_msg('注册成功.',array('name'=>trim($this->req['username']),'login_key'=>$cache_key));
        }
        else
            $this->error('注册失败.');
    }
    // --------------------------------------------------------------------

	
}